/*******************************************************************************
 * Copyright (c) baifeiq.
 * 
 * @date            @author         @brief
 * 2022-01-01       baifeiq         First version
 */

#include "os_define.h"
#include "os_common.h"

/* Bitmap. */
static const os_uint8_t os_lowest_bitmap[256] = 
{
		/*0 , 			1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, */
/*0*/	0xFF,	        0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 
/*1*/	4,    			0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/*2*/	5,    			0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/*3*/	4,    			0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/*4*/	6,    			0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/*5*/	4,    			0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/*6*/	5,    			0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/*7*/	4,    			0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/*8*/	7,    			0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/*9*/	4,    			0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/*A*/	5,    			0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/*B*/	4,    			0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/*C*/	6,    			0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/*D*/	4,    			0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/*E*/	5,    			0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/*F*/	4,    			0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
};

/**
 * @brief Get the highest priority for the ready task.
 * 
 * @param value Task ready table.
 */
os_uint8_t os_get_bitmap_lowest(os_uint32_t value)
{
	if (0 == value)
		return 0xFF;

	if (value & 0xFF)
		return os_lowest_bitmap[(value & 0xFF)];

	if (value & 0xFF00)
		return os_lowest_bitmap[(value & 0xFF00)>>8] + 8;

	if ((value & 0xFF0000))
		return os_lowest_bitmap[(value & 0xFF0000)>>16] + 16;

	return os_lowest_bitmap[(value & 0xFF000000)>>24] + 24;
}
